﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Fis.Win.Forms
{
    public partial class BookReportForm : ReportBaseForm
    {
        public BookReportForm()
        {
            InitializeComponent();
            InitializeViewUI();
            InitializeBinding();
        }

        /// <summary>
        /// 数据绑定
        /// </summary>
        public override void InitializeBinding()
        {
            var xml = Agile.Engine.ObjectContext.GetResource("SqlResource");

            var sql = xml.GetString("report_book");

            var data = Agile.Engine.Data.DataContext.Get("CacheDataContext").DefaultModel.QueryTable(CommandType.Text, "BookReport", sql);
            var dataRow = data.Select(string.Format("Isnull<>{0}", 0));
            if (dataRow.Length != 0)
            {
                gridControl2.DataSource = dataRow.CopyToDataTable();
            }
        }

        public override void InitializeViewUI()
        {
            iView.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            theGridControl = gridControl2;
        }

        protected override void ShowFilter()
        {
            var f = new ReportFilterForm();

            if (f.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                ///获取周期过滤条件
                RangeFilter = (f.RangeFilter == null) ? (int.Parse(Fis.Win.Utils.AppSession.Book["FBOOKRANGE"].ToString()) - 1).ToString() : f.RangeFilter.ToString();
                ///获取科目过滤条件
                AccFilter = (f.AccountFilter == null) ? "null" : f.AccountFilter.ToString();
                var xml = Agile.Engine.ObjectContext.GetResource("SqlResource");
                var sql = xml.GetString("report_book");
                gridControl2.DataSource = null;
                var data = Agile.Engine.Data.DataContext.Get("CacheDataContext").DefaultModel.QueryTable(CommandType.Text, "BookReport", sql);
                var dataRow = (DataRow[])null;
                if (AccFilter == "null")
                {
                    dataRow = data.Select(string.Format("FRANGE in({0}) and Isnull<>{1}", RangeFilter, 0));
                }
                else
                {
                    dataRow = data.Select(string.Format("FACCCODE='{0}' and FRANGE in({1}) and Isnull<>{2}", AccFilter, RangeFilter, 0));
                }
                if (dataRow.Length != 0)
                {
                    gridControl2.DataSource = dataRow.CopyToDataTable();
                }
            }
        }

        /// <summary>
        /// 不需要实现
        /// </summary>
        /// <param name="data"></param>
        protected override void InitializeResetColumns(DataTable data)
        {

        }
    }
}
